﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Aspose.Pdf.Devices;
using System.Reflection;
using Aspose.Words.Rendering;
using System.Drawing;
using System.Drawing.Text;
using Helpers;
using Aspose.Cells;
using Aspose.Cells.Rendering;


namespace OfficeApplication
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string Search()
        {
            string filPath = string.Empty;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "C:\\";//只是打开窗口时定位。
            openFileDialog1.Filter = "文件(*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            var b = openFileDialog1.ShowDialog();
            if (b != null && b.Value)
            {
                filPath = openFileDialog1.FileName;
            }
            else
            {
            }
            return filPath;
        }


        string[] Paths = new string[] { };
        string Paths1;
        int i = 0;

        private void open_Click(object sender, RoutedEventArgs e)
        {
            string filepath = Search();
            string extension = System.IO.Path.GetExtension(filepath);
            if (extension == ".ppt" || extension == ".pptx" || extension == ".pptm")
            {
                Paths = PPTHelper.ConvertToImages(filepath).ToArray();// Directory.GetFiles(Paths1);
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));
            }
            else if (extension == ".pdf")
            {
                Paths = PDFHelper.ConvertToImages(filepath).ToArray();// Directory.GetFiles(Paths1);
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));


            }
            else if (extension == ".docx" || extension == ".doc")
            {
                Paths = WordHelper.ConvertToImages(filepath).ToArray();// Directory.GetFiles(Paths1);
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));
            }
            else if (extension == ".xls" || extension == ".xlsx")
            {
                int count = 0;
                var bmps = ExcelHelper.ConvertToImages(filepath);
                foreach (var item in bmps)
                {
                    count++;
                    item.Save(@"C:\codes\OfficeDecoder\111_" + count + ".jpg");
                }
            }
        }

        private void last_Click(object sender, RoutedEventArgs e)
        {
            i--;
            if (i > 0 && i < Paths.Length)
            {
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));
            }
            else
            {
                i = Paths.Length - 1;
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));
            }
        }

        private void next_Click(object sender, RoutedEventArgs e)
        {
            i++;
            if (i < Paths.Length)
            {
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));

            }
            else
            {
                i = 0;
                this.image.Source = new BitmapImage(new Uri(Paths[i], UriKind.RelativeOrAbsolute));

            }
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("保存成功");
        }
    }
}
